package com.hhy.hhy_weibo.controller;

import com.hhy.hhy_weibo.common.api.ApiResult;
import com.hhy.hhy_weibo.jwt.JwtUtil;
import com.hhy.hhy_weibo.model.domain.BmsComment;
import com.hhy.hhy_weibo.model.domain.BmsPost;
import com.hhy.hhy_weibo.model.domain.UmsUser;
import com.hhy.hhy_weibo.model.dto.CommentDTO;
import com.hhy.hhy_weibo.model.vo.CommentVO;
import com.hhy.hhy_weibo.serviec.IBmsCommentService;
import com.hhy.hhy_weibo.serviec.IUmsUserService;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


@RestController
@RequestMapping("/comment")
public class BmsCommentController extends BaseController {

    private static final String USER_NAME = JwtUtil.USER_NAME;
    @Resource
    private IBmsCommentService bmsCommentService;
    @Resource
    private IUmsUserService umsUserService;

    @GetMapping("/get_comments")
    public ApiResult<List<CommentVO>> getCommentsByTopicID(@RequestParam(value = "topicid", defaultValue = "1") String topicid) {
        List<CommentVO> lstBmsComment = bmsCommentService.getCommentsByTopicID(topicid);
        return ApiResult.success(lstBmsComment);
    }
    @PostMapping("/add_comment")
    public ApiResult<BmsComment> add_comment(@RequestHeader(value = USER_NAME) String userName,
                                             @RequestBody CommentDTO dto) {
        UmsUser user = umsUserService.getUserByUsername(userName);
        BmsComment comment = bmsCommentService.create(dto, user);
        return ApiResult.success(comment);
    }

    @DeleteMapping("/delete/{id}")
    public ApiResult<String> delete(@PathVariable("id") String id) {
        bmsCommentService.removeById(id);
        return ApiResult.success(null,"评论删除成功");
    }

    @PostMapping("/add_parentcomment")
    public ApiResult<BmsComment> add_parentcomment(@RequestHeader(value = USER_NAME) String userName,
                                                   @RequestBody CommentDTO dto) {
        UmsUser user = umsUserService.getUserByUsername(userName);
        BmsComment comment = bmsCommentService.create(dto, user);
        return ApiResult.success(comment);
    }

    @GetMapping("/get_allcomments")
    public ApiResult<List<BmsComment>> getallcomments() {
        List<BmsComment> list = bmsCommentService.getallcomments();
//        for (BmsComment comment : list) {
//            System.out.println(comment);
//        }
        return ApiResult.success(list);
    }
}
